package tabletask

import (
	"cvevulner/cve-timed-task/db_models"
	"cvevulner/models"
	"fmt"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"strconv"
	"time"
)

// RepeatTask Parse the artificial CVE table, crawl the CVE official
// website data, and store it in the database
func RepeatTask() {
	ormModel := orm.NewOrm()
	logs.Info("CVE with empty CVE official website data is re-crawled ")

	originExcel, rowsAffected, err := db_models.SelectCveOriginExcel("", ormModel)
	if err != nil {
		logs.Error("db_models.SelectCveOriginExcel error:", err)
		return
	}
	if rowsAffected == 0 {
		return
	}
	now := time.Now().Format("2006-01-02 15:04:05")
	for _, v := range originExcel {
		url := fmt.Sprintf("https://nvd.nist.gov/vuln/detail/%s", v.CveNum)
		crawlList, err := Crawling(url)
		if err != nil {
			logs.Error("Crawling error:", err, url)
			continue
		}
		rep := GetRepair(v.CveNum)
		if len(rep) > 0 {
			crawlList.RepairTime = rep
		}
		nvdScore, err := strconv.ParseFloat(crawlList.NvdScore, 64)
		if err != nil {
			logs.Error(err.Error())
		}
		err = db_models.UpdateCveOriginExcelTypeFour(models.OriginExcel{
			NVDScore:          nvdScore,
			CveLevel:          crawlList.CveLevel,
			CveDesc:           crawlList.CveDesc,
			RepairTime:        crawlList.RepairTime,
			NVDVector:         crawlList.VectorValue,
			AttackVector:      crawlList.AttackVector,
			AccessVector:      crawlList.AccessVector,
			AttackComplexity:  crawlList.AttackComplexity,
			AccessComplexity:  crawlList.AccessComplexity,
			PrivilegeRequired: crawlList.PrivilegeRequired,
			UserInteraction:   crawlList.UserInteraction,
			Scope:             crawlList.Scope,
			Confidentiality:   crawlList.Confidentiality,
			Integrity:         crawlList.Integrity,
			Availability:      crawlList.Availability,
			Authentication:    crawlList.Authentication,
			CveStatus:         1,
			UpdateTime:        now,
			ScoreType:         crawlList.ScoreType,
			CveNum:            v.CveNum,
			PackName:          v.PackName,
			CveVersion:        v.CveVersion,
		}, ormModel)
		if err != nil {
			logs.Error("db_models.UpdateCveOriginExcelTypeFour error:", err)
		}
	}
}

// GetPublishedDateTask Get the release date of nvd
func GetPublishedDateTask() {
	ormModel := orm.NewOrm()
	logs.Info("Get the release date of nvd")
	vulnCenter, rowsAffected, err := db_models.SelectCveVulnCenter(ormModel)
	if err != nil {
		logs.Error("db_models.SelectCveVulnCenter error:", err)
		return
	}
	if rowsAffected == 0 {
		return
	}
	for _, v := range vulnCenter {
		url := fmt.Sprintf("https://nvd.nist.gov/vuln/detail/%s", v.CveNum)
		crawlList, err := Crawling(url)
		if err != nil {
			logs.Error("Crawling error:", err, url)
			continue
		}
		rep := GetRepair(v.CveNum)
		if len(rep) > 0 {
			crawlList.RepairTime = rep
		}
		if crawlList.RepairTime != "" {
			err = db_models.UpdateCveVulnCenter(crawlList.RepairTime, strconv.FormatInt(v.CveId, 10), ormModel)
			if err != nil {
				logs.Error("db_models.UpdateCveVulnCenter error:", err)
				continue
			}
		}
	}
}
